package com.code.petapp.activity;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.content.Intent;
import android.os.Bundle;
import android.view.animation.AccelerateDecelerateInterpolator;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.code.petapp.R;
import com.code.petapp.bean.UserBean;
import com.code.petapp.databinding.ActivityRegisterBinding;
import com.code.petapp.http.HttpListener;
import com.code.petapp.util.KeyboardUtils;
import com.code.petapp.util.UiUtils;
import com.code.petapp.util.UserUtils;

import io.reactivex.rxjava3.annotations.Nullable;

public class RegisterActivity extends BaseActivity {
    ActivityRegisterBinding inflate;
    private int viewBottom;
    private ObjectAnimator animatorUp, animatorDown;
    private int translationY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        inflate = ActivityRegisterBinding.inflate(getLayoutInflater());
        setContentView(inflate.getRoot());
        Glide.with(this).load(R.drawable.icon)
                .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                .into(inflate.logoR);
        setRegister();
        inflate.imgBack.setOnClickListener(view -> finish());
        initKeyBoardListener();
    }

    private void setRegister() {
        inflate.btnReg.setOnClickListener(view -> {
            String userName = inflate.etAc.getText().toString();
            String password = inflate.etPw1.getText().toString();
            String passwordAg = inflate.etPw2.getText().toString();
            if (userName.isEmpty() || password.isEmpty() || passwordAg.isEmpty()) {
                toastShortShow("请完善注册信息");
            } else if (!password.equals(passwordAg)) {
                toastShortShow("两次输入密码不一致");
            } else {
                //发送登录请求
                UserBean userBean = new UserBean();
                userBean.setUsername(userName);
                userBean.setPassword(password);
                UserUtils.register(userBean, new HttpListener<>() {
                    @Override
                    public void success(@Nullable UserBean object) {
                        toastShortShow("注册成功");
                        Intent intent = new Intent(RegisterActivity.this, StartActivity.class);
                        startActivity(intent);
                        finish();
                    }

                    @Override
                    public void error(int code, @Nullable String error) {
                        toastShortShow(error);
                    }
                });
            }
        });
    }

    private void initKeyBoardListener() {
        inflate.inputItem.post(() -> {
            int[] viewLocation = new int[2];
            inflate.inputItem.getLocationOnScreen(viewLocation);
            viewBottom = UiUtils.INSTANCE.getScreenHeight(RegisterActivity.this) - viewLocation[1] - inflate.inputItem.getHeight();
        });
        KeyboardUtils.INSTANCE.setKeyboardVisibilityListenerAuto(this, (isVisible, keyboardHeight) -> {
            if (isVisible) {
                if (animatorUp == null) {
                    translationY = keyboardHeight - viewBottom;
                    animatorUp = ObjectAnimator.ofFloat(inflate.inputItem, "translationY", 0, -translationY);
                    animatorUp.setDuration(360);
                    animatorUp.setInterpolator(new AccelerateDecelerateInterpolator());
                } else {
                    animatorUp.setFloatValues(inflate.inputItem.getTranslationY(), -translationY);
                }
                animatorUp.start();
                logoAnimator(true);
            } else {
                if (animatorDown == null) {
                    animatorDown = ObjectAnimator.ofFloat(inflate.inputItem, "translationY", -translationY, 0);
                    animatorDown.setDuration(360);
                    animatorDown.setInterpolator(new AccelerateDecelerateInterpolator());
                } else {
                    animatorDown.setFloatValues(inflate.inputItem.getTranslationY(), 0);
                }
                animatorDown.start();
                if (translationY != 0) {
                    logoAnimator(false);
                }
            }
        });
    }

    private void logoAnimator(boolean isBig) {
        PropertyValuesHolder scaleX;
        PropertyValuesHolder scaleY;
        PropertyValuesHolder tY;
        if (isBig) {
            scaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 0.5f);
            scaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 0.5f);
            tY = PropertyValuesHolder.ofFloat("translationY", 0f, 200f);
        } else {
            scaleX = PropertyValuesHolder.ofFloat("scaleX", 0.5f, 1f);
            scaleY = PropertyValuesHolder.ofFloat("scaleY", 0.5f, 1f);
            tY = PropertyValuesHolder.ofFloat("translationY", 200f, 0f);
        }
        ObjectAnimator.ofPropertyValuesHolder(inflate.logoR, scaleX, scaleY, tY)
                .setDuration(360)
                .start();
    }
}